package com.edu.springboot_disk_erban.controller;

import com.edu.springboot_disk_erban.entity.ResultModel;
import com.edu.springboot_disk_erban.entity.User;
import com.edu.springboot_disk_erban.service.UserService;
import net.minidev.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/registry")
    @ResponseBody
    public ResultModel<Integer> registry(@RequestBody User user){
        ResultModel<Integer> model;
        // System.out.println(user);

        int num = userService.registry(user);
        if (num > 0){
            model = ResultModel.success("注册成功", num);
        }else{
            model = ResultModel.error("用户名重复了！换个名字吧!");
        }

        return model;
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResultModel<User> getLogin(@RequestBody User user){
        ResultModel<User> model;
        //前端传给后端的值
        // System.out.println(user+"+++++");
        //findUser 数据库里查询的值
        User findUser = userService.findUser(user);
        // System.out.println(findUser+"-----");
        if (findUser != null){
            //把findUser传给前端
            model = ResultModel.success("登录成功",findUser);
        }else{
            model = ResultModel.error("登陆失败，用户名或用户密码错误！");
        }

        return model;
    }

    @GetMapping("findUserInfo")
    @ResponseBody
    public ResultModel<User> findUserInfo(String username){
        System.out.println(username);
        ResultModel<User> model;
        User user = userService.findUserInfo(username);
        System.out.println(user);
        model = ResultModel.success("查询成功", user);
        return model;
    }

    @RequestMapping(value = "/editUserInfo")
//    @PostMapping("editUserInfo")
    @ResponseBody
    public ResultModel<User> editUserInfo(@RequestBody User user){
        System.out.println("------我我噢噢噢噢");
        userService.editUserInfo(user);
        ResultModel<User> model;
        model = ResultModel.success("修改成功");
        System.out.println(model);
        return model;
    }

    @PostMapping("editPasswd")
    @ResponseBody
    public void editPasswd(@RequestBody User user){
        System.out.println("----++++++");
        System.out.println(user);
        //System.out.println("------我我噢噢噢噢");
        userService.editPasswd(user);
        ResultModel<User> model;
        model = ResultModel.success("提交成功");
        System.out.println(model);
    }

}
